﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml;
using SiteMapEditor.Forms.Entities;
using Microsoft.Xrm.Sdk;
using SiteMapEditor.Forms.WebRessources;

namespace SiteMapEditor.Controls
{
   
    public partial class SubAreaControl : UserControl, ISiteMapSavable
    {
        private Dictionary<string,string> collec;

        private string initialEntity = "";
        private string initialGetStartedPanePath = "";
        private string initialGetStartedPanePathAdmin = "";
        private string initialGetStartedPanePathOutlook = "";
        private string initialGetStartedPanePathAdminOutlook = "";
        private string initialIcon = "";
        private string initialId = "";
        private string initialUrl = "";
        private string initialTitle = "";
        private string initialDescription = "";
        
        private bool initialAvailableOffline = false;
        private bool initialPassParams = false;
        private bool initialClientOutlook = false;
        private bool initialClientOutlookLaptopClient = false;
        private bool initialClientOutlookWorkstationClient = false;
        private bool initialClientWeb = false;
        private bool initialSkuAll = false;
        private bool initialSkuLive = false;
        private bool initialSkuOnPremise = false;
        private bool initialSkuSPLA = false;

        #region Delegates

        public delegate void SaveEventHandler(object sender, SaveEventArgs e);

        #endregion

        #region Event Handlers

        public event SaveEventHandler Saved;

        #endregion

        public SubAreaControl()
        {
            InitializeComponent();

            this.collec = new Dictionary<string, string>();

            ToolTip tip = new ToolTip();
            tip.ToolTipTitle = "Information";
            tip.SetToolTip(this.chkSubAreaAvailableOffline, "Controls whether SubArea is available offline.");
            tip.SetToolTip(this.chkSubAreaPassParams, "Specifies whether information about the organization and language context are passed to the URL.");
            tip.SetToolTip(this.txtOutlookShortcutIcon, "Specifies the icon to display in Microsoft Dynamics CRM for Microsoft Office Outlook.");
            tip.SetToolTip(this.txtSubAreaEntity, "Specifies the name for the entity. If a Url is not specified, the default view of the specified entity will be displayed.");
            tip.SetToolTip(this.txtSubAreaGetStartedPanePath, "Specifies the path to the Get Started page for this subarea.");
            tip.SetToolTip(this.txtSubAreaGetStartedPanePathAdmin, "Specifies the path to the Get Started page for this subarea if the user is logged in as an administrator.");
            tip.SetToolTip(this.txtSubAreaGetStartedPanePathAdminOutlook, "Specifies the path to the Get Started page for this subarea if the user is logged in as an administrator and Microsoft Dynamics CRM for Outlook is in use.");
            tip.SetToolTip(this.txtSubAreaGetStartedPanePathOutlook, "Specifies the path to the Get Started page for this subarea when Microsoft Dynamics CRM for Outlook is in use.");
            tip.SetToolTip(this.txtSubAreaIcon, "Specifies a URL for an 18x18 pixel image to display for the SubArea.");
            tip.SetToolTip(this.txtSubAreaId, "A unique identifier for this SubArea element.\r\n\r\nValid values: a-z, A-Z, 0-9, and underscore (_)");
            tip.SetToolTip(this.txtSubAreaUrl, "Specifies a URL or HTML Web Resource for a page to display in the main frame of the application when this subarea is selected.");
            tip.SetToolTip(this.txtSubAreaTitle, "Deprecated. Use the <Titles> (SiteMap) and <Title> (SiteMap) elements.");
            tip.SetToolTip(this.txtSubAreaDescription, "Deprecated. Use the <Description> (SiteMap) element.");
        }

        public SubAreaControl(Dictionary<string, string> collection)
            : this()
        {

            this.collec = collection;

            this.FillControls();
        }

        private void FillControls()
        {
            this.txtSubAreaEntity.Text = collec.ContainsKey("Entity") ? collec["Entity"]: "";
            this.txtSubAreaGetStartedPanePath.Text = collec.ContainsKey("GetStartedPanePath") ? collec["GetStartedPanePath"]: "";
            this.txtSubAreaGetStartedPanePathAdmin.Text = collec.ContainsKey("GetStartedPanePathAdmin") ? collec["GetStartedPanePathAdmin"] : "";
            this.txtSubAreaGetStartedPanePathAdminOutlook.Text = collec.ContainsKey("GetStartedPanePathAdminOutlook") ? collec["GetStartedPanePathAdminOutlook"]: "";
            this.txtSubAreaGetStartedPanePathOutlook.Text = collec.ContainsKey("GetStartedPanePathOutlook") ? collec["GetStartedPanePathOutlook"] : "";
            this.txtSubAreaIcon.Text = collec.ContainsKey("Icon") ? collec["Icon"] : "";
            this.txtSubAreaId.Text = collec.ContainsKey("Id") ? collec["Id"] : "";
            this.txtSubAreaUrl.Text = collec.ContainsKey("Url") ? collec["Url"] : "";
            this.txtSubAreaResourceId.Text = collec.ContainsKey("ResourceId") ? collec["ResourceId"] : "";
            this.txtSubAreaDescriptionResourceId.Text = collec.ContainsKey("DescriptionResourceId") ? collec["DescriptionResourceId"] : "";
            this.txtSubAreaTitle.Text = collec.ContainsKey("Title") ? collec["Title"] : "";
            this.txtSubAreaDescription.Text = collec.ContainsKey("Description") ? collec["Description"] : "";

            this.initialEntity = this.txtSubAreaEntity.Text;
            this.initialGetStartedPanePath = this.txtSubAreaGetStartedPanePath.Text;
            this.initialGetStartedPanePathAdmin = this.txtSubAreaGetStartedPanePathAdmin.Text;
            this.initialGetStartedPanePathOutlook = this.txtSubAreaGetStartedPanePathOutlook.Text;
            this.initialGetStartedPanePathAdminOutlook = this.txtSubAreaGetStartedPanePathAdminOutlook.Text;
            this.initialIcon = this.txtSubAreaIcon.Text;
            this.initialId = this.txtSubAreaId.Text;
            this.initialUrl = this.txtSubAreaUrl.Text;
            this.initialDescription = this.txtSubAreaDescription.Text;
            this.initialTitle = this.txtSubAreaTitle.Text;

            this.chkSubAreaAvailableOffline.Checked = collec.ContainsKey("AvailableOffline") ? collec["AvailableOffline"].ToLower() == "true" : false;
            this.chkSubAreaPassParams.Checked = collec.ContainsKey("PassParams") ? collec["PassParams"].ToLower() == "true" : false;
            this.chkSubAreaClientOutlook.Checked = collec.ContainsKey("Client") ? collec["Client"].IndexOf("Outlook") >= 0 : false;
            this.chkSubAreaClientOutlookLaptopClient.Checked = collec.ContainsKey("Client") ? collec["Client"].IndexOf("OutlookLaptopClient") >= 0 : false;
            this.chkSubAreaClientOutlookWorkstationClient.Checked = collec.ContainsKey("Client") ? collec["Client"].IndexOf("OutlookWorkstationClient") >= 0 : false;
            this.chkSubAreaClientWeb.Checked = collec.ContainsKey("Client") ? collec["Client"].IndexOf("Web") >= 0 : false;
            this.chkSubAreaSkuAll.Checked = collec.ContainsKey("Sku") ? collec["Sku"].IndexOf("All") >= 0 : false;
            this.chkSubAreaSkuLive.Checked = collec.ContainsKey("Sku") ? collec["Sku"].IndexOf("Live") >= 0 : false;
            this.chkSubAreaSkuOnPremise.Checked = collec.ContainsKey("Sku") ? collec["Sku"].IndexOf("OnPremise") >= 0 : false;
            this.chkSubAreaSkuSPLA.Checked = collec.ContainsKey("Sku") ? collec["Sku"].IndexOf("SPLA") >= 0 : false;

            this.initialAvailableOffline = this.chkSubAreaAvailableOffline.Checked;
            this.initialPassParams= this.chkSubAreaPassParams.Checked;
            this.initialClientOutlook = this.chkSubAreaClientOutlook.Checked;
            this.initialClientOutlookLaptopClient = this.chkSubAreaClientOutlookLaptopClient.Checked;
            this.initialClientOutlookWorkstationClient = this.chkSubAreaClientOutlookWorkstationClient.Checked;
            this.initialClientWeb = this.chkSubAreaClientWeb.Checked;
            this.initialSkuAll = this.chkSubAreaSkuAll.Checked;
            this.initialSkuLive= this.chkSubAreaSkuLive.Checked;
            this.initialSkuOnPremise = this.chkSubAreaSkuOnPremise.Checked;
            this.initialSkuSPLA = this.chkSubAreaSkuSPLA.Checked;
        }

        private void SubAreaControl_Leave(object sender, EventArgs e)
        {
            if (this.initialEntity != this.txtSubAreaEntity.Text ||
            this.initialGetStartedPanePath != this.txtSubAreaGetStartedPanePath.Text ||
            this.initialGetStartedPanePathAdmin != this.txtSubAreaGetStartedPanePathAdmin.Text ||
            this.initialGetStartedPanePathOutlook != this.txtSubAreaGetStartedPanePathOutlook.Text ||
            this.initialGetStartedPanePathAdminOutlook != this.txtSubAreaGetStartedPanePathAdminOutlook.Text ||
            this.initialIcon != this.txtSubAreaIcon.Text ||
            this.initialId != this.txtSubAreaId.Text ||
            this.initialUrl != this.txtSubAreaUrl.Text ||
            this.initialDescription != this.txtSubAreaDescription.Text ||
            this.initialTitle != this.txtSubAreaTitle.Text ||
            this.initialAvailableOffline != this.chkSubAreaAvailableOffline.Checked ||
            this.initialClientOutlook != this.chkSubAreaClientOutlook.Checked ||
            this.initialClientOutlookLaptopClient != this.chkSubAreaClientOutlookLaptopClient.Checked ||
            this.initialClientOutlookWorkstationClient != this.chkSubAreaClientOutlookWorkstationClient.Checked ||
            this.initialClientWeb != this.chkSubAreaClientWeb.Checked ||
            this.initialSkuAll != this.chkSubAreaSkuAll.Checked ||
            this.initialSkuLive != this.chkSubAreaSkuLive.Checked ||
            this.initialSkuOnPremise != this.chkSubAreaSkuOnPremise.Checked ||
            this.initialSkuSPLA != this.chkSubAreaSkuSPLA.Checked)
            {
                if (MessageBox.Show("You didn't save your changes! Do you want to save them now?", "Question", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                {
                    this.Save();
                }
            }
        }

        public void Save()
        {
            if (this.txtSubAreaId.Text.Length == 0)
            {
                MessageBox.Show(this, "Id is required!", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
            else
            {
                Dictionary<string, string> collection = new Dictionary<string, string>();
                if (this.txtSubAreaEntity.Text.Length > 0)
                    collection.Add("Entity", this.txtSubAreaEntity.Text);
                if (this.txtSubAreaGetStartedPanePath.Text.Length > 0)
                    collection.Add("GetStartedPanePath", this.txtSubAreaGetStartedPanePath.Text);
                if (this.txtSubAreaGetStartedPanePathAdmin.Text.Length > 0)
                    collection.Add("GetStartedPanePathAdmin", this.txtSubAreaGetStartedPanePathAdmin.Text);
                if (this.txtSubAreaGetStartedPanePathAdminOutlook.Text.Length > 0)
                    collection.Add("GetStartedPanePathAdminOutlook", this.txtSubAreaGetStartedPanePathAdminOutlook.Text);
                if (this.txtSubAreaGetStartedPanePathOutlook.Text.Length > 0)
                    collection.Add("GetStartedPanePathOutlook", this.txtSubAreaGetStartedPanePathOutlook.Text);
                if (this.txtSubAreaIcon.Text.Length > 0)
                    collection.Add("Icon", this.txtSubAreaIcon.Text);
                if (this.txtSubAreaId.Text.Length > 0)
                    collection.Add("Id", this.txtSubAreaId.Text);
                if (this.txtSubAreaUrl.Text.Length > 0)
                    collection.Add("Url", this.txtSubAreaUrl.Text);
                if (this.txtSubAreaResourceId.Text.Length > 0)
                    collection.Add("ResourceId", this.txtSubAreaResourceId.Text);
                if (this.txtSubAreaDescriptionResourceId.Text.Length > 0)
                    collection.Add("DescriptionResourceId", this.txtSubAreaDescriptionResourceId.Text);
                if (this.txtSubAreaDescription.Text.Length > 0)
                    collection.Add("Description", this.txtSubAreaDescription.Text);
                if (this.txtSubAreaTitle.Text.Length > 0)
                    collection.Add("Title", this.txtSubAreaTitle.Text);
                collection.Add("AvailableOffline", this.chkSubAreaAvailableOffline.Checked.ToString().ToLower());
                collection.Add("PassParams", this.chkSubAreaPassParams.Checked.ToString().ToLower());

                string client = "";

                if (this.chkSubAreaAll.Checked)
                    client += "All";
                if (this.chkSubAreaClientOutlook.Checked)
                    client += ",Outlook";
                if (this.chkSubAreaClientOutlookLaptopClient.Checked)
                    client += ",OutlookLaptopClient";
                if (this.chkSubAreaClientOutlookWorkstationClient.Checked)
                    client += ",OutlookWorkstationClient";
                if (this.chkSubAreaClientWeb.Checked)
                    client += ",Web";

                if (client.StartsWith(","))
                    client = client.Remove(0, 1);

                if (client.Length > 0)
                    collection.Add("Client", client);

                string sku = "";

                if (this.chkSubAreaSkuAll.Checked)
                    sku += "All";
                if (this.chkSubAreaSkuLive.Checked)
                    sku += ",Live";
                if (this.chkSubAreaSkuOnPremise.Checked)
                    sku += ",OnPremise";
                if (this.chkSubAreaSkuSPLA.Checked)
                    sku += ",SPLA";

                if (sku.StartsWith(","))
                    sku = sku.Remove(0, 1);

                if (sku.Length > 0)
                    collection.Add("Sku", sku);

                this.initialEntity = this.txtSubAreaEntity.Text;
                this.initialGetStartedPanePath = this.txtSubAreaGetStartedPanePath.Text;
                this.initialGetStartedPanePathAdmin = this.txtSubAreaGetStartedPanePathAdmin.Text;
                this.initialGetStartedPanePathOutlook = this.txtSubAreaGetStartedPanePathOutlook.Text;
                this.initialGetStartedPanePathAdminOutlook = this.txtSubAreaGetStartedPanePathAdminOutlook.Text;
                this.initialIcon = this.txtSubAreaIcon.Text;
                this.initialId = this.txtSubAreaId.Text;
                this.initialUrl = this.txtSubAreaUrl.Text;
                this.initialDescription = this.txtSubAreaDescription.Text;
                this.initialTitle = this.txtSubAreaTitle.Text;

                this.initialAvailableOffline = this.chkSubAreaAvailableOffline.Checked;
                this.initialPassParams = this.chkSubAreaPassParams.Checked;
                this.initialClientOutlook = this.chkSubAreaClientOutlook.Checked;
                this.initialClientOutlookLaptopClient = this.chkSubAreaClientOutlookLaptopClient.Checked;
                this.initialClientOutlookWorkstationClient = this.chkSubAreaClientOutlookWorkstationClient.Checked;
                this.initialClientWeb = this.chkSubAreaClientWeb.Checked;
                this.initialSkuAll = this.chkSubAreaSkuAll.Checked;
                this.initialSkuLive = this.chkSubAreaSkuLive.Checked;
                this.initialSkuOnPremise = this.chkSubAreaSkuOnPremise.Checked;
                this.initialSkuSPLA = this.chkSubAreaSkuSPLA.Checked;

                this.SendSaveMessage(collection);
            }
        }

        #region Send Events

        /// <summary>
        /// Sends a connection success message 
        /// </summary>
        /// <param name="service">IOrganizationService generated</param>
        /// <param name="parameters">Lsit of parameter</param>
        private void SendSaveMessage(Dictionary<string, string> collection)
        {
            SaveEventArgs sea = new SaveEventArgs() { AttributeCollection = collection };

            if (this.Saved != null)
            {
                this.Saved(this, sea);
            }
        }

        #endregion

        private void buttonSelectEntity_Click(object sender, EventArgs e)
        {
            EntityPicker picker = new EntityPicker();
            picker.StartPosition = FormStartPosition.CenterParent;

            if (picker.ShowDialog() == DialogResult.OK)
            {
                this.txtSubAreaEntity.Text = picker.SelectedEntity;
            }
        }

        private void btnBrowsIcon_Click(object sender, EventArgs e)
        {
            WebResourcePicker wrp = new WebResourcePicker(WebResourcePicker.WebResourceType.Image);
            wrp.StartPosition = FormStartPosition.CenterParent;

            if (wrp.ShowDialog() == DialogResult.OK)
            {
                this.txtSubAreaIcon.Text = "$webresource:" + wrp.SelectedResource;
            }
        }

        private void buttonBrowseUrl_Click(object sender, EventArgs e)
        {
            WebResourcePicker wrp = new WebResourcePicker(WebResourcePicker.WebResourceType.WebPage);
            wrp.StartPosition = FormStartPosition.CenterParent;

            if (wrp.ShowDialog() == DialogResult.OK)
            {
                this.txtSubAreaUrl.Text = "$webresource:" + wrp.SelectedResource;
            }
        }
    }
}
